#include "rpc_server/Rpc_server.hpp"
#include "db_common/db.hpp"
#include "db_common/Log.hpp"
#include "db_common/Util.hpp"

int main()
{
    user_table ut("8.153.171.255", "weiqi_admin", "weiqi194312", "online_weiqi", 33360);

    std::unique_ptr<Rpc::Server::SDescribeFactory> desc_factory_insert(new Rpc::Server::SDescribeFactory());
    desc_factory_insert->setMethodName("user_insert");
    desc_factory_insert->setParamsDesc("password", Rpc::Server::VType::STRING);
    desc_factory_insert->setParamsDesc("username", Rpc::Server::VType::STRING);
    desc_factory_insert->setReturnType(Rpc::Server::VType::OBJECT);
    desc_factory_insert->setCallback(std::bind(&user_table::insert, &ut, std::placeholders::_1, std::placeholders::_2));

    std::unique_ptr<Rpc::Server::SDescribeFactory> desc_factory_login(new Rpc::Server::SDescribeFactory());
    desc_factory_login->setMethodName("user_login");
    desc_factory_login->setParamsDesc("password", Rpc::Server::VType::STRING);
    desc_factory_login->setParamsDesc("username", Rpc::Server::VType::STRING);
    desc_factory_login->setReturnType(Rpc::Server::VType::OBJECT);
    desc_factory_login->setCallback(std::bind(&user_table::login, &ut, std::placeholders::_1, std::placeholders::_2));

    std::unique_ptr<Rpc::Server::SDescribeFactory> desc_factory_select_by_name(new Rpc::Server::SDescribeFactory());
    desc_factory_select_by_name->setMethodName("select_by_name");
    desc_factory_select_by_name->setParamsDesc("username", Rpc::Server::VType::STRING);
    desc_factory_select_by_name->setReturnType(Rpc::Server::VType::OBJECT);
    desc_factory_select_by_name->setCallback(std::bind(&user_table::select_by_name, &ut, std::placeholders::_1, std::placeholders::_2));

    std::unique_ptr<Rpc::Server::SDescribeFactory> desc_factory_select_by_id(new Rpc::Server::SDescribeFactory());
    desc_factory_select_by_id->setMethodName("select_by_id");
    desc_factory_select_by_id->setParamsDesc("uid", Rpc::Server::VType::INTEGRAL);
    desc_factory_select_by_id->setReturnType(Rpc::Server::VType::OBJECT);
    desc_factory_select_by_id->setCallback(std::bind(&user_table::select_by_id, &ut, std::placeholders::_1, std::placeholders::_2));

    std::unique_ptr<Rpc::Server::SDescribeFactory> desc_factory_win(new Rpc::Server::SDescribeFactory());
    desc_factory_win->setMethodName("win");
    desc_factory_win->setParamsDesc("uid", Rpc::Server::VType::INTEGRAL);
    desc_factory_win->setReturnType(Rpc::Server::VType::OBJECT);
    desc_factory_win->setCallback(std::bind(&user_table::win, &ut, std::placeholders::_1, std::placeholders::_2));

    std::unique_ptr<Rpc::Server::SDescribeFactory> desc_factory_lose(new Rpc::Server::SDescribeFactory());
    desc_factory_lose->setMethodName("lose");
    desc_factory_lose->setParamsDesc("uid", Rpc::Server::VType::INTEGRAL);
    desc_factory_lose->setReturnType(Rpc::Server::VType::OBJECT);
    desc_factory_lose->setCallback(std::bind(&user_table::lose, &ut, std::placeholders::_1, std::placeholders::_2));
        
    Rpc::Server::RpcServer server(Rpc::Address("8.153.171.255", 51218), true, Rpc::Address("118.193.33.170", 31218));
    server.registerMethod(desc_factory_insert->build());
    server.registerMethod(desc_factory_login->build());
    server.registerMethod(desc_factory_select_by_name->build());
    server.registerMethod(desc_factory_select_by_id->build());
    server.registerMethod(desc_factory_win->build());
    server.registerMethod(desc_factory_lose->build());
    server.start();
    return 0;
}
